package com.anbao.newanbao;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.anbao.action.ActivityManagerAction;
import com.beardedhen.androidbootstrap.BootstrapButton;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.Toast;

public class QueryAlarmActivity extends Activity {
	private ImageView left_img;
	private BootstrapButton queryAlarmBtn;
	
	private DatePicker startDate;
	
	private DatePicker stopDate;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		  requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_query_warn);
		//将当前界面加入到系统管理中国
		ActivityManagerAction.getScreenManager().pushActivity(this);
		iniView();
	}
	private void iniView() {
		queryAlarmBtn = (BootstrapButton) findViewById(R.id.queryAlarmBtn);
		queryAlarmBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				
				
				queryData();
		
			}
		});
		
		
		startDate = (DatePicker) findViewById(R.id.startDate);
		
		stopDate = (DatePicker) findViewById(R.id.stopDate);
		
		left_img = (ImageView) findViewById(R.id.left_img);
		left_img.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
//				Intent intent = new Intent(UpdataUserPwdActivity.this,SettingActivity.class);
//				startActivity(intent);
				QueryAlarmActivity.this.finish();
			}
		});
	}
	protected void queryData() {
		
		new Thread(new Runnable() {
			
			@Override
			public void run() {

				
				String  result=  httpReq() ;
				 
				 
			       Message msg = new Message();
		            Bundle bundle = new Bundle();
		            bundle.putString("data", result);
		            msg.setData(bundle);
		            handler.sendMessage(msg);
			}
		}).start();
		
		
	
		
	}
	
	
	private String  httpReq() {

    	int startYear = startDate.getYear();
    	int startMonth = startDate.getMonth();
    	int startday = startDate.getDayOfMonth();
    	
    	int stoptYear = stopDate.getYear();
    	int stoptMonth = stopDate.getMonth();
    	int stoptday = stopDate.getDayOfMonth();
    	
    	StringBuffer sburl = new StringBuffer();
    	sburl.append("http://192.168.0.115:8080/AnBaoWebServer/alarmServlet");
    	sburl.append("?actionType=getData");
    	sburl.append("&startDate="+startYear+"-"+startMonth+"-"+startday);
    	sburl.append("&stopDate="+stoptYear+"-"+stoptMonth+"-"+stoptday);
     	sburl.append("&userId=1");
 	   // 使用GET方法发送请求,需要把参数加在URL后面，用？连接，参数之间用&分隔
     
     // 生成请求对象
     HttpGet httpGet = new HttpGet(sburl.toString());
     HttpClient httpClient = new DefaultHttpClient();

     // 发送请求
     try
     {
         HttpResponse response = httpClient.execute(httpGet);
        return  responseResult(response);
     }
     catch (Exception e)
     {
    	 e.printStackTrace();
        return null;
     }
 	
	}



/**
* 显示收到的HTTP请求结果
* @param response
*/
	private String  responseResult(HttpResponse response) {

		 if (null == response)
	        {
	            return null;
	        }
	        HttpEntity httpEntity = response.getEntity();
	        try
	        {
	            InputStream inputStream = httpEntity.getContent();
	            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
	            String result = "";
	            String line = "";
	            while (null != (line = reader.readLine()))
	            {
	                result += line;
	            }
	            
	            Log.e("请求数据", result);
	            return result;
	     
	            
	    
//	            Toast.makeText(MainActivity.this,result , 1).show();   
	        }catch (Exception e)
	        {
	        	e.printStackTrace();
	        	  return null;
	        }
	        
	     
		
	}


 private Handler handler = new Handler(){
	 
	 @Override
	public void handleMessage(Message msg) {

		
		Bundle data =  msg.getData();
		
		if(data.getString("data")!=null){
			
			Intent intent = new Intent(QueryAlarmActivity.this, QueryAlarmResultActivity.class);
			intent.putExtra("data", data.getString("data"));
			startActivity(intent);
		}else{
			
			Toast.makeText(QueryAlarmActivity.this, "链接出现异常", 1).show();
			
		}
		
	}
	 
 };

}
